<input id="current-view-url" type="hidden" value="<?=\yii\helpers\Url::to()?>">
<?php
$js = <<<JS
    var index=0;
    loadData();
    function loadData(){
        var param=new Object();
        var url=$('#current-view-url').val();

        if($('#search-handle').length>0){
            param.searchData=getElementValueObject('search');
        }
        $("tbody").empty().remove();
        $(".example-advanced").find('thead').after("<tbody id='tree-tbody'></tbody>");
        var tbody = $('#tree-tbody'), props = [];
        var treefield=$(".example-advanced").data('treefield');//获取那一列要展成树状
        $.each($('.example-advanced th'), function(i, prop){
             props[i]=$(this).attr('scope');
        });

        $.post(url,{_csrf:$('meta[name=csrf-token]').attr('content'),param:JSON.stringify(param)},function(data){
            getTree(data.data,props,tbody,treefield);
            //树表格
            setTimeout(function(){
            $(".example-advanced").treetable({expandable: true,clickableNodeNames:true,indent:38});
            $(".example-advanced").treetable("expandAll");
            },1000);
        },'json');
    }
    //为选中的行更改背景色
    $(".example-advanced").on('click','tbody tr',function() {
        $(this).addClass("zx_table_selected").siblings().removeClass("zx_table_selected");
    });
    function getTree(data,props,tbody,treefield){
        $.each(data, function(i, value){
            var tr = $('<tr>');
            if(value['tt-parent-id']==''){
                tr.attr('data-tt-id',value['id']);
            }else{
                tr.attr('data-tt-parent-id',value['tt-parent-id']+'');
                tr.attr('data-tt-id',value['tt-parent-id']+'-'+value['id']);
            }
            $.each(props, function(j, prop){
                if(prop=='treetable-number'){
                     index+=1;
                     $('<td>').attr('data-field',prop).html(index).appendTo(tr);
                }else if(prop==treefield){
                     if(value.hasOwnProperty('children')&& value['children']!='') {
                          $('<td>').addClass('yx-text-left1').attr('data-field',prop).html('<span class="folder">'+value[prop]+'</span>').appendTo(tr);
                     }else{
                          $('<td>').addClass('yx-text-left1').attr('data-field',prop).html('<span class="file">'+value[prop]+'</span>').appendTo(tr);
                     }
                }else{
                     $('<td>').attr('data-field',prop).html(value[prop]).appendTo(tr);
                }
            });
            tbody.append(tr);
            if(value.hasOwnProperty('children')&& value['children']!='') {
                getTree(value['children'],props,tbody,treefield);
            }
        });
    }


    function reloadData(){
        loadData();
    }
    $('#search-handle').click(function(){
        loadData();
    });

JS;
$this->registerJS($js);
?>